import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/teacher_work_train_details_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/eacher_work_train_list_controller.dart';

///教师的培训
class TeacherWorkTrainListPage extends StatefulWidget {
  final String tag;

  final bool shrinkWrap;

  const TeacherWorkTrainListPage(
      {Key key, this.shrinkWrap = false, @required this.tag})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TeacherWorkTrainListPageState();
  }
}

class _TeacherWorkTrainListPageState extends State<TeacherWorkTrainListPage>
    with SingleTickerProviderStateMixin {
  TeacherTrainListController _historyController;
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _historyController = Get.put(
      TeacherTrainListController(),
      tag: widget.tag,
    );
    _tabController = new TabController(length: 2, vsync: this);

    Future.delayed(Duration.zero, () async {
      await _historyController.requestListFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的培训"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TeacherTrainListController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _historyController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    List<TeacherTrainBean> _list = _historyController.getList();

    if (_list == null || _list.length == 0) {
      return Center(
        child: NoDataWidget(
          text: "暂无参与的培训",
          clickCallBack: () {
            _historyController.requestListFunction(click: true);
          },
        ),
      );
    }
    if (widget.shrinkWrap) {
      return buildListView();
    }
    return RefreshIndicator(
      onRefresh: () async {
        await _historyController.requestListFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: buildListView(),
    );
  }

  AnimatedSwitcher buildListViewAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(
      ///动画执行切换时间
      duration: const Duration(milliseconds: 200),

      ///动画构建器 构建指定动画类型
      transitionBuilder: (Widget child, Animation<double> animation) {
        //执行缩放动画
        return FadeTransition(child: child, opacity: animation);
      },

      ///执行动画的子 Widget
      ///只有子 Widget 被切换时才会触发动画
      child: Container(
        key: UniqueKey(),
        child: buildMyListWidget(),
      ),
    );
  }

  int _currentIndex = 0;

  buildMyListWidget() {
    if (_currentIndex == 0) {
      return buildListView();
    }
    return Padding(
      padding: EdgeInsets.only(left: 10),
    );
  }

  buildListView() {
    List<TeacherTrainBean> _list = _historyController.getList();
    return ListView.builder(
      padding: EdgeInsets.zero,
      shrinkWrap: widget.shrinkWrap,
      physics: widget.shrinkWrap
          ? NeverScrollableScrollPhysics()
          : BouncingScrollPhysics(),
      //条目个数
      itemCount: _list.length,
      //每个子条目的样式
      itemBuilder: (BuildContext context, int index) {
        TeacherTrainBean teacherTrainBean = _list[index];
        TrainBean trainBean = teacherTrainBean.trainBean;
        return GestureDetector(
          onTap: () {
            Get.to(() => TeacherWorkTrainDetailsPage(
                  tag: trainBean.trainId.toString(),
                  trainId: trainBean.trainId,
                ));
          },
          child: Container(
            color: Colors.white,
            margin: EdgeInsets.only(bottom: 10),
            padding: EdgeInsets.all(12),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  children: [
                    Expanded(
                      child: Text(
                        trainBean.trainName,
                        style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w600),
                      ),
                    )
                  ],
                ),
                SizedBox(height: 8),
                Row(
                  children: [
                    Text("开始时间:"),
                    Text(
                      "${trainBean.startStudyTime} ",
                      style: TextStyle(
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                Row(
                  children: [
                    Text("结束时间:"),
                    Text(
                      "${trainBean.endStudyTime} ",
                      style: TextStyle(
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
